<?php
require_once('App-Top.php');
require_once('Authorizer.php');
require_once('DBConfigQuery.php');
require_once('GenFunctions.php');
require_once('../config.inc.php');
require_once('../Validation.php');

// Check if it is Member Mng
if (isset($_SESSION['SESS_USER_TYPE'])) {
    if (!isUserType_Members($_SESSION['SESS_USER_TYPE'])) {
        header("Location: AdminIndex.php");
        exit();
    }
}

// Check if the Get is set and not empty and that complies with the right number of digits
if ( !isset($_GET['membid']) ) {
    if (isset($_SESSION['FORMS']['FormEditMember'])){ unset ($_SESSION['FORMS']['FormEditMember']); }
    header("Location: AdminMemberEdit.php");
    exit();
}
if ( empty($_GET['membid']) ) {
    if (isset($_SESSION['FORMS']['FormEditMember'])){ unset ($_SESSION['FORMS']['FormEditMember']); }
    header("Location: AdminMemberEdit.php");
    exit();
}
if ( (strlen($_GET['membid']) < MIN_CHARS_USERNAME_PASSW) || (strlen($_GET['membid']) > MAX_CHARS_USERNAME_PASSW) ) {
    if (isset($_SESSION['FORMS']['FormEditMember'])){ unset ($_SESSION['FORMS']['FormEditMember']); }
    header("Location: AdminMemberEdit.php");
    exit();
}

$q = sprintf($Q_ALL_Member, $_GET['membid']);
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_num_rows($r) == 1) {
    $_SESSION['FORMS']['FormEditMember'] = mysqli_fetch_array($r, MYSQLI_ASSOC);
    mysqli_free_result($r);
    mysqli_close($dbc);
} else {
    // Free DB
    if (isset($_SESSION['FORMS']['FormEditMember'])){ unset ($_SESSION['FORMS']['FormEditMember']); }
    mysqli_free_result($r);
    mysqli_close($dbc);
    die("Query failed");
}


//TODO: Validate fields and show errors here...

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Member Edit</title>
        <link href="StyleControl.css" rel="stylesheet" type="text/css" />
    </head>
    <body>

        <div id="sidebar">
            <h3>Reports</h3>
            <ul>
                <li><a href="#">Gate List</a></li>
                <li><a href="#">Members List</a></li>
            </ul>


            <?php
            require_once('MenuAdmin.php');
            ?>
        </div>

        <div id="mainbody">
            <h1>Member Edition</h1> <br />

            <form id="editMember" name="editMember" method="POST" action="AdminMemberEditConfirm.php" class="compactform">
                <fieldset class="clearfix">

                    <div class="twoper">
                        <label for="fname">First Name
                        <input name="fname" type="text" id="fname" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['FirstName'])) { echo $_SESSION['FORMS']['FormEditMember']['FirstName']; } ?>"  /></label>


                        <label for="lname">Last Name
                        <input name="lname" type="text" id="lname" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['LastName'])) { echo $_SESSION['FORMS']['FormEditMember']['LastName']; } ?>"  /></label>

                    </div>

                    <div class="threeper">
                        <label for="email">Email
                        <input  name="email" type="text" id="email" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['Email'])) { echo $_SESSION['FORMS']['FormEditMember']['Email']; } ?>"  /></label>


                        <label for="login">Username
                        <input  name="login" type="text" id="login" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['MemberId'])) { echo $_SESSION['FORMS']['FormEditMember']['MemberId']; } ?>"  /></label>


                        <label for="password">Password
                        <input   name="password" type="text" id="password" value="<?php /* if (isset($_SESSION['FORMS']['FormEditMember']['Password'])) { echo $_SESSION['FORMS']['FormEditMember']['Password']; } */ ?>"  /></label>

                    </div>

                    <div class="threeper">
                        <label for="cphone">Cell Phone
                        <input name="cphone" type="text" id="cphone" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['CellPhone'])) { echo $_SESSION['FORMS']['FormEditMember']['CellPhone']; } ?>"  /></label>


                        <label for="hphone">Home Phone
                        <input name="hphone" type="text" id="hphone" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['HomePhone'])) { echo $_SESSION['FORMS']['FormEditMember']['HomePhone']; } ?>"  /> <small>(Optional)</small> </label>


                        <label for="ophone">Office Phone
                        <input name="ophone" type="text" id="ophone" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['OfficePhone'])) { echo $_SESSION['FORMS']['FormEditMember']['OfficePhone']; } ?>"  /> <small>(Optional)</small> </label>

                    </div>

                    <div class="twoper">
                        <label for="profession">Profession
                        <input name="profession" type="text" id="profession" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['Profession'])) { echo $_SESSION['FORMS']['FormEditMember']['Profession']; } ?>" /> <small>(Optional)</small> </label>


                        <label for="education">Education
                        <input name="education" type="text" id="education" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['Education'])) { echo $_SESSION['FORMS']['FormEditMember']['Education']; } ?>" /> <small>(Optional)</small> </label>

                    </div>

                    <div class="twoper">
                        <label for="family">Family Information
                        <input name="family" type="text" id="family" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['Family'])) { echo $_SESSION['FORMS']['FormEditMember']['Family']; } ?>"  /> <small>(Optional)</small> </label>


                        <label for="associations">Associations
                        <input name="associations" type="text" id="associations" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['Associations'])) { echo $_SESSION['FORMS']['FormEditMember']['Associations']; } ?>" /> <small>(Optional)</small> </label>

                    </div>

                    <div class="oneper">
                        <label for="interests">Interests
                        <input name="interests" type="text" id="interests" value="<?php if (isset($_SESSION['FORMS']['FormEditMember']['Interests'])) { echo $_SESSION['FORMS']['FormEditMember']['Interests']; } ?>" /> <small>(Optional)</small> </label>

                    </div>

                    <div class="threeper">
                        <label for="status">Status
                        <select name="status" id="status" style="width:11em"> <?php if (isset($_SESSION['FORMS']['FormEditMember']['Status'])) { if ( (strtolower($_SESSION['FORMS']['FormEditMember']['Status'])) != 'i') {
                                                                                                                                echo '<option selected="yes">Active</option> <option>Inactive</option>';}
                                                                                                                             else {
                                                                                                                                echo '<option selected="yes">Inactive</option> <option>Active</option>';} }?> </select> </label>
                    </div>

                    <div class="oneper">
                        <input type="submit" name="EditMember" value="Edit Member" class="button"/>
                    </div>

                </fieldset>
            </form>


            <!-- TODO: Put the conditions for the password -->
            <br />
            <p class="note">The Password appears empty for security reasons. If you want to change it, please type a new password with the following properties: </p>

            <?php
            if (isset($_SESSION['ERRMSG_ARR']))
            {
                echo '<p class="err">Please correct the following errors: </p>';

                $errmsg_arr = $_SESSION['ERRMSG_ARR'];
                foreach ($errmsg_arr as $value) {
                    echo "<p class='err'>- $value</p>";
                }

                unset ($_SESSION['ERRMSG_ARR']);
            }

            ?>


        </div>

    </body>
</html>
<?php

    require_once('App-End.php');
?>

<!-- Designed: Jose Trujillo - 2011 -->